﻿using System;
using Microsoft.Xna.Framework;
using XNAGraffiti.ParticleSystems;

namespace XNAGraffiti
{
    public class PaintGraffiti :Graffiti
    {
        private ParticleSystem m_paintParticles;

        public PaintGraffiti(Game game) : base(game)
        {
            m_paintParticles = new PaintParticleSystem(Game, Game.Content);
            m_paintParticles.DrawOrder = 580;
            game.Components.Add(m_paintParticles);
        }

        public override void DrawLine(Point? startPoint, Point? endPoint)
        {
            if (!startPoint.HasValue || !endPoint.HasValue)
                return;

            var points = GetLinePoints(startPoint.Value, endPoint.Value);

            for (int i = 0; i < points.Length; i++)
            {
                var linePoint = points[i];

                Vector3 point = ScreenPointToVector3(linePoint);

                var rnd = new Random((int)DateTime.Now.Ticks);

                m_paintParticles.AddParticle(point, Vector3.Zero);
            }
        }

        public override void Draw(GameTime gameTime)
        {
            float aspectRatio = (float)Game.GraphicsDevice.Viewport.Width /
                                (float)Game.GraphicsDevice.Viewport.Height;

            Matrix view = Matrix.CreateTranslation(0, -25, 0) *
                          Matrix.CreateRotationY(MathHelper.ToRadians(CameraRotation)) *
                          Matrix.CreateRotationX(MathHelper.ToRadians(CameraArc)) *
                          Matrix.CreateLookAt(new Vector3(0, 0, -CameraDistance),
                                              new Vector3(0, 0, 0), Vector3.Up);

            Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
                                                                    aspectRatio,
                                                                    1, 10000);

            m_paintParticles.SetCamera(view, projection);
        }
    }
}
